package com.tenton.servlet;

import com.tenton.pojo.User;
import com.tenton.service.Impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Date: 2021/1/13
 * @Author: Tenton
 * @Description: 修改用户
 */
@WebServlet(name = "UpdateUserServlet",urlPatterns = "/updateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        UserServiceImpl userServiceImpl = new UserServiceImpl();
        //用户Id
        int id = Integer.parseInt(request.getParameter("id"));
        //用户名
        String userName = request.getParameter("userName");
        //密码
        String password = request.getParameter("password");
        //邮箱
        String email = request.getParameter("email");
        //职位
        String prositionName = request.getParameter("prositionName");
        //省
        String province = request.getParameter("province");
        //市
        String city = request.getParameter("city");
        //爱好
        String hobby = request.getParameter("hobby");
        User user = userServiceImpl.getUserById(id);
        user = new User(userName, password, email,prositionName, province, city, hobby);
        boolean result = userServiceImpl.updateUser(user);
        response.getWriter().write("修改失败，2秒后自动跳转到主界面");
        response.setHeader("Refresh","3;URL=/listUserServlet");
        if (result){
            request.getRequestDispatcher("/listUserServlet").forward(request,response);
        }else {
            response.getWriter().write("修改失败，2秒后自动跳转到主界面");
            response.setHeader("Refresh","3;URL=/listUserServlet");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
